package com.newbingo.dao;

import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.Criteria;
import org.hibernate.criterion.Expression;
import org.hibernate.criterion.Projections;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.newbingo.dao.imple.ITradeRecordDao;
import com.newbingo.entity.TradeRecord;
import com.newbingo.util.Pager;

public class TradeRecordDao extends HibernateDaoSupport implements ITradeRecordDao {
	private static final Log log = LogFactory.getLog(TradeRecordDao.class);

	/* 
	 *查询所有交易记录
	 */
	public List<TradeRecord> findAll(){
		log.debug("find all TradeRecord instance");
		try{
			Criteria cri=getSession().createCriteria(TradeRecord.class);
			cri.setResultTransformer(Criteria.ROOT_ENTITY);
			return cri.list();
		}catch(RuntimeException re){
			log.error("finding TradeRecord instance failed");
			throw re;
		}
	}
	
	/* 
	 * 根据会员ID分页查询出交易历史记录
	 */
	public Pager findByPage(int start,int limit,Integer uno){
		log.debug("find all TradeRecord instance by condition and page");
		try{
			Pager page=new Pager();
			Criteria cri=getSession().createCriteria(TradeRecord.class);
			if(uno!=null){
				cri.createAlias("member", "member");
				cri.add(Expression.eq("member.uno",uno));
			}
			cri.setProjection(Projections.rowCount());
			page.setTotalSize(((Number)cri.uniqueResult()).intValue());
			cri.setProjection(null);
			cri.setFirstResult(0);
			cri.setMaxResults(limit);
			cri.setResultTransformer(Criteria.ROOT_ENTITY);
			page.setData(cri.list());
			log.debug("find all TradeRecord instance by condition and page successfully");
			return page;
		}catch(RuntimeException re){
			log.error("finding TradeRecord List failed");
			throw re;
		}
	}
	
	/*
	 * 保存交易信息
	 */
	public void save(TradeRecord tradeRecord){
		log.debug("saving TradeRecord instance");
		try{
			getSession().save(tradeRecord);
			log.debug("save successfully");
		}catch(RuntimeException re){
			log.error("save failed");
			throw re;
		}
	}
	
}
